home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Samples / C++ / Direct3D / CompiledEffect / CompiledEffect.fx < prev    next >
Encoding:
Text File  |  2004-09-27  |  4.7 KB  |  138 lines

  1. //-----------------------------------------------------------------------------
  2. // File: CompiledEffect.fx
  3. //
  4. // Desc: 
  5. // 
  6. // Copyright (c) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8.  
  9.  
  10. //-----------------------------------------------------------------------------
  11. // Global variables
  12. //-----------------------------------------------------------------------------
  13. float4 g_MaterialAmbientColor;    // Material's ambient color
  14. float4 g_MaterialDiffuseColor;    // Material's diffuse color
  15.  
  16. // This effect file uses a single directional light
  17. float3 g_LightDir = normalize(float3(1.0f, 1.0f, 1.0f));    // Light's direction
  18. float4 g_LightAmbient = { 1.0f, 1.0f, 1.0f, 1.0f };            // Light's ambient color
  19. float4 g_LightDiffuse = { 1.0f, 1.0f, 1.0f, 1.0f };            // Light's diffuse color
  20.  
  21. texture g_RenderTargetTexture;    // Full screen render target texture 
  22. texture g_MeshTexture;            // Color texture for mesh
  23.  
  24. float     g_fTime;                    // App's time in seconds
  25. float4x4 g_mWorld;                    // World matrix for object
  26. float4x4 g_mWorldViewProjection;    // World * View * Projection matrix
  27.  
  28.  
  29.  
  30. //-----------------------------------------------------------------------------
  31. // Texture samplers
  32. //-----------------------------------------------------------------------------
  33. sampler RenderTargetSampler = 
  34. sampler_state
  35. {
  36.     Texture = <g_RenderTargetTexture>;
  37.     MinFilter = POINT;  
  38.     MagFilter = POINT;
  39.     MipFilter = NONE;
  40.     AddressU = Clamp;
  41.     AddressV = Clamp;
  42. };
  43.  
  44. sampler MeshTextureSampler = 
  45. sampler_state
  46. {
  47.     Texture = <g_MeshTexture>;    
  48.     MipFilter = LINEAR;
  49.     MinFilter = LINEAR;
  50.     MagFilter = LINEAR;
  51. };
  52.  
  53.  
  54. //-----------------------------------------------------------------------------
  55. // Vertex shader output structure
  56. //-----------------------------------------------------------------------------
  57. struct VS_OUTPUT
  58. {
  59.     float4 Position   : POSITION;   // vertex position 
  60.     float4 Diffuse    : COLOR0;     // vertex diffuse color
  61.     float2 TextureUV  : TEXCOORD0;  // vertex texture coords 
  62. };
  63.  
  64.  
  65. //-----------------------------------------------------------------------------
  66. // Name: RenderSceneVS
  67. // Type: Vertex shader                                      
  68. // Desc: This shader computes standard transform and lighting
  69. //-----------------------------------------------------------------------------
  70. VS_OUTPUT RenderSceneVS( float4 vPos : POSITION, 
  71.                          float3 vNormal : NORMAL,
  72.                          float2 vTexCoord0 : TEXCOORD0 )
  73. {
  74.     VS_OUTPUT Output;
  75.     float3 vNormalWorldSpace;
  76.   
  77.     // Animation the vertex based on time and the vertex's object space position
  78.     float4 vAnimatedPos = vPos + float4( sin(g_fTime) * vPos.x/2, 0, 0, 0 );
  79.     
  80.     // Transform the position from object space to homogeneous projection space
  81.     Output.Position = mul(vAnimatedPos, g_mWorldViewProjection);
  82.     
  83.     // Transform the normal from object space to world space    
  84.     vNormalWorldSpace = normalize(mul(vNormal, (float3x3)g_mWorld)); // normal (world space)
  85.     
  86.     // Compute simple directional lighting equation
  87.     Output.Diffuse.rgb = g_MaterialDiffuseColor * g_LightDiffuse * max(0,dot(vNormalWorldSpace, g_LightDir)) + 
  88.                          g_MaterialAmbientColor * g_LightAmbient;   
  89.     Output.Diffuse.a = 1.0f; 
  90.     
  91.     // Just copy the texture coordinate through
  92.     Output.TextureUV = vTexCoord0; 
  93.     
  94.     return Output;    
  95. }
  96.  
  97.  
  98. //-----------------------------------------------------------------------------
  99. // Pixel shader output structure
  100. //-----------------------------------------------------------------------------
  101. struct PS_OUTPUT
  102. {
  103.     float4 RGBColor : COLOR0;  // Pixel color    
  104. };
  105.  
  106.  
  107. //-----------------------------------------------------------------------------
  108. // Name: RenderScenePS                                        
  109. // Type: Pixel shader
  110. // Desc: This shader outputs the pixel's color 
  111. //-----------------------------------------------------------------------------
  112. PS_OUTPUT RenderScenePS( VS_OUTPUT In ) 
  113.     PS_OUTPUT Output;
  114.  
  115.     // Lookup mesh texture and modulate it with diffuse
  116.     Output.RGBColor = tex2D(MeshTextureSampler, In.TextureUV) * In.Diffuse;
  117.  
  118.     return Output;
  119. }
  120.  
  121.  
  122.  
  123. //-----------------------------------------------------------------------------
  124. // Name: RenderScene
  125. // Type: Technique                                     
  126. // Desc: Renders scene to render target
  127. //-----------------------------------------------------------------------------
  128. technique RenderScene
  129. {
  130.     pass P0
  131.     {          
  132.         ZENABLE = true;
  133.         VertexShader = compile vs_1_1 RenderSceneVS();
  134.         PixelShader  = compile ps_1_1 RenderScenePS();
  135.     }
  136. }
  137.